<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Codemodel extends CI_Model {

	protected $_CI;

	public static $searchField = array(
		'code_type' => 'code_type',
	);
	public static $likeField = array(
		'code_name' => 'code_name',
		'code_desc' => 'code_desc',
	);

	public function __construct(){
		parent::__construct();
		$this->_CI = & get_instance();
	}
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
	public function getCodeListSection($assist){
		$this->db->from('code');
		$this->db->where('code_status !=',STATUS_DELETE);
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		$count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

		if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
		$this->db->order_by('code_sort','desc');
		$query = $this->db->get();
		$list = $query->result_array();

		return array($list,$count);
	}

	public function getCodeList($assist){
		$this->db->from('code');
		$this->db->where('code_status !=',STATUS_DELETE);
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		$this->db->order_by('code_sort','desc');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getCode($code_id){
		$this->db->from('code');
		$this->db->where('code_id',$code_id);
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}

	public function getCodeByIds($code_ids){
		if(!is_array($code_ids)) $code_ids = array($code_ids);
		if(empty($code_ids)) return array();
		$this->db->from('code');
		$this->db->where_in('code_id',$code_ids);
		$this->db->where('code_status !=',STATUS_DELETE);
		$this->db->order_by('code_sort','desc');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getNeedCodeList($code_ids) {
		$this->db->from('code');
		$this->db->where_in('code_id',$code_ids);
		$this->db->where('code_status',STATUS_ACTIVE);
		$this->db->order_by('code_sort','desc');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getCodeSpriteList($code_id){
		$this->db->from('code_sprite');
		$this->db->where('code_id',$code_id);
		$this->db->where('sprite_status !=',STATUS_DELETE);
		$this->db->order_by('sprite_x','asc');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getSprite($sprite_id){
		$this->db->from('code_sprite');
		$this->db->where('sprite_id',$sprite_id);
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}

/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
	public function createCode($info){
		$this->db->insert('code',$info);
		return $this->db->insert_id();
	}
	public function createCodeSpriteBatch($info){
		if(!empty($info)){
			$this->db->insert_batch('code_sprite',$info);
		}
	}
/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
	public function editCode($info){
		$this->db->where('code_id',$info['code_id']);
		unset($info['code_id']);
		$this->db->update('code',$info);
	}

	public function editCodeBatch($info){
		if(!empty($info)){
			$this->db->update_batch('code',$info,'code_id');
		}
	}
	public function editCodeSpriteBatch($info){
		$this->db->update_batch('code_sprite',$info,'sprite_id');
		return true;
	}
/*
| -------------------------------------------------------------------
|  DELETE Functions
| -------------------------------------------------------------------
*/
/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/
	public function checkNameAvailable($name,$code_id = 0){
		$this->db->from('code');
		$this->db->where('code_id !=',$code_id);
		$this->db->where('code_name',$name);
		$count = $this->db->count_all_results();

		return ($count == 0);
	}
/*
| -------------------------------------------------------------------
|  INIT Functions
| -------------------------------------------------------------------
*/
	public function initCodeFolder($code_id) {
		if(!file_exists(CODE_IMAGE_PATH.$code_id)) {
			$old = umask(0);
			@mkdir(CODE_IMAGE_PATH.$code_id,0777,true);
			umask($old);
		} elseif(!file_exists(CODE_IMAGE_PATH.$code_id.'/sprite')) {
			$old = umask(0);
			@mkdir(CODE_IMAGE_PATH.$code_id.'/sprite',0777,true);
			umask($old);
		}
		return true;
	}
/*
| -------------------------------------------------------------------
|  GENERATE Functions
| -------------------------------------------------------------------
*/
	public function generateSprite($code,$width,$height){
		$sprite_list = $this->getCodeSpriteList($code['code_id']);
		foreach($sprite_list as $key => &$record){
			$record['sprite_path'] = CODE_IMAGE_PATH.$record['sprite_path'];
			if($record['sprite_status'] != STATUS_ACTIVE || !file_exists($record['sprite_path'])) {
				unset($sprite_list[$key]);
				continue;	
			}
			$info = getimagesize($record['sprite_path']);
			$record['sprite_width'] = $info[0];
			$record['sprite_height'] = $info[1];
			$right = $record['sprite_width'] + $record['sprite_x'];
			$down = $record['sprite_height'] + $record['sprite_y'];
		}
        $canvas = new Imagick();
        $canvas->newImage($width,$height,new ImagickPixel('transparent'));

        foreach($sprite_list as $sprite){
            $src = new Imagick($sprite['sprite_path']);
            $canvas->compositeImage($src,imagick::COMPOSITE_DEFAULT,$sprite['sprite_x'],$sprite['sprite_y']);
            $src->clear();
            $src->destroy();
		}
        $canvas->setImageFormat('png');
        $canvas->writeImage(CODE_IMAGE_PATH.$code['code_id'].'/sprite.png');
        $canvas->clear();
        $canvas->destroy();
	}
/*
| -------------------------------------------------------------------
|  OUTPUT Functions
| -------------------------------------------------------------------
*/
}